package com.molichuxing.services.business.service.impl;

import com.molichuxing.services.business.service.CarsMileageMonitoringBizService;
import com.molichuxing.services.infrastructure.dto.request.create.CarsMileageMonitoringEnterpriseDto;
import com.molichuxing.services.infrastructure.dto.request.create.CarsMileageWarnEnterpriseDto;
import com.molichuxing.services.infrastructure.dto.response.CarsMileageMonitoringEnterpriseResDto;
import com.molichuxing.services.infrastructure.service.ICarsMileageMonitoringEnterpriseService;
import com.molichuxing.services.infrastructure.service.ICarsMileageWarnEnterpriseService;
import com.molichuxing.services.property.BasicEnum;
import com.molichuxing.services.property.HandlerEnum;
import com.sun.nio.sctp.HandlerResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 添加车联报警
 *
 * @author wanghualin
 * @date 2020 -06-11 15:47:48
 */
@Service("carsMileageMonitoringBizService")
@Transactional(rollbackFor = Exception.class)
public class CarsMileageMonitoringBizServiceImpl implements CarsMileageMonitoringBizService {
    @Autowired
    private ICarsMileageMonitoringEnterpriseService enterpriseService;
    @Autowired
    private ICarsMileageWarnEnterpriseService warnEnterpriseService;

    @Override
    public void saveCarsMileage(CarsMileageMonitoringEnterpriseDto dto) {
        enterpriseService.saveCarsMileageMonitoringEnterprise(dto);
        genWarn(dto);
    }

    @Override
    public void updateCarsMileage(CarsMileageMonitoringEnterpriseDto dto) {
        enterpriseService.updateCarsMileageMonitoringEnterprise(dto);
        genWarn(dto);
    }
    private void genWarn(CarsMileageMonitoringEnterpriseDto dto) {
        if (dto.getDriveMileage()!=null&&dto.getDriveMileage()>dto.getMileageUpper()&&dto.getMileageUpper()!=0){
            CarsMileageWarnEnterpriseDto dto1=new CarsMileageWarnEnterpriseDto();
            dto1.setMonitoringId(dto.getId());
            dto1.setStatus(HandlerEnum.NO.getValue());
            dto1.setBeyondMileage(dto.getDriveMileage()-dto.getMileageUpper());
            dto1.setGmtCreate(LocalDateTime.now());
            warnEnterpriseService.saveCarsMileageWarnEnterprise(dto1);
        }
    }
}
